<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\material;

use app\model\PicModel;
use app\model\ZcStoreModel;
use app\model\ZcTaskModel;
use app\service\BaseService;
use app\service\company\DepartmentService;
use app\service\creative\StoreService;
use app\service\creative\TaskService;
use app\service\material\base\StaffTrait;
use app\service\material\base\TaskTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class PicService extends BaseService
{
    use TaskTrait;
    use StaffTrait;

    /**
     * 获取图片创意相关人员.
     *
     * @param PicModel $model 图片模型
     * @return array|bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getPicCreativeStaff(PicModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }

        // 非系统任务图片
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }

        $storeModel = StoreService::getStoreById($taskModel->getAttr('store_id'));
        if (! $storeModel instanceof ZcStoreModel || ! $storeModel->getKey()) {
            return self::setError('未知店铺');
        }

        $subtaskId = intval($model->getAttr('subtask_id'));
        //        $teamId = intval($model->getAttr('team_id'));
        //        $departmentId = intval($model->getAttr('department_id'));

        // 获取创意参与人
        $staffs = self::getCreativeStaff($subtaskId);
        // 获取创意组负责人
        //        $staffs = array_merge($staffs, self::getCreativeTeamStaff($teamId));
        // 获取创意部门负责人
        //        $staffs = array_merge($staffs, self::getCreativeDeptStaff($departmentId));
        return array_unique(array_diff($staffs, ['']));
    }

    /**
     * 获取图片任务运营发起人.
     *
     * @param PicModel $model 图片模型
     * @return array|bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getPicTaskOperation(PicModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }
        // 非系统任务视频
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }
        return intval($taskModel->getAttr('create_uid'));
    }

    /**
     * 获取图片任务创意团队.
     *
     * @param PicModel $model 图片模型
     * @return array|bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getPicTaskTeam(PicModel $model)
    {
        if (! $model->getKey()) {
            return [];
        }
        // 非系统任务图片
        if (! self::isTask($model)) {
            return [];
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }
        return intval($taskModel->getAttr('team_id'));
    }

    /**
     * 获取图片所属区域
     *
     * @param PicModel $model 视频模型
     * @return bool|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getPicDepartmentArea(PicModel $model)
    {
        if (! $model->getKey()) {
            return 0;
        }
        // 非系统任务图片
        if (! self::isTask($model)) {
            return 0;
        }

        $taskModel = TaskService::getTaskById($model->getAttr('task_id'));
        if (! $taskModel instanceof ZcTaskModel || ! $taskModel->getKey()) {
            return self::setError('未知任务');
        }

        $storeModel = StoreService::getStoreById($taskModel->getAttr('store_id'));
        if (! $storeModel instanceof ZcStoreModel || ! $storeModel->getKey()) {
            return self::setError('未知店铺');
        }
        $departmentId = intval($storeModel->getAttr('department_id'));
        return DepartmentService::getAreaByCache($departmentId);
    }
}
